package com.itheima.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utills.CommonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author ${Azer}
 * @LOVE,,,
 * @Date 2021/9/22 20:33
 */
@WebServlet("/User")
public class UserServlet extends BaseServlet {

    /**
     * 根据关键字搜索用户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void  searchUserByKeywords(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取表单提交的数据
        String keyword = request.getParameter("keyword");
        System.out.println("keyword : " + keyword);

        //实例化service层调用方法
        UserService userService = new UserService();
        List<User> users = userService.searchUserByKeywords(keyword);

        //把用户数据存储到request域对象中
        request.setAttribute("userlist", users);

        //请求转发
        request.getRequestDispatcher("/search.jsp").forward(request, response);

    }
    /**
     * 删除用户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从浏览器获取要删除的id
        int userid =Integer.parseInt(request.getParameter("id"));
        System.out.println("要删除的用户编号："+userid);

        //实例化业务层
        UserService userService=new UserService();

        boolean result = userService.deleteUserById(userid);

        if (result){
            //删除成功
            response.sendRedirect("/delete_ok.jsp");
        }else {
            //删除失败
            response.getWriter().println("<h1>删除失败！3秒后跳转首页。。。</h1>");
            response.setHeader("refresh","3;url=//localhost:8080/index.jsp");
        }
    }

        /**
         * （修改用户）
         * 根据id查询用户 ，把用户数据回显到修改页面上
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
         */
    public void findUserById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取修改用户的id
        int id = Integer.parseInt(request.getParameter("id"));
        System.out.println("用户id="+id);

        UserService userService=new UserService();
        User user = userService.findUserById(id);

        //把用户对象存储到request中
        request.setAttribute("user",user);

        //转发到修改页面
        request.getRequestDispatcher("/update.jsp").forward(request,response);

    }

        /**
         * 修改用户
         *
         * @param
         */
    public void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取用户更新页面提交的数据,并封装成User对象
        User user = CommonUtils.CommonPopulate(User.class, request);
        System.out.println("用户修改的数据为："+user);

        UserService userService = new UserService();

        boolean result = userService.updateUser(user);

        if(result){
            //修改成功
            response.sendRedirect("/update_ok.jsp");

        }else {
            //修改失败
            response.getWriter().println("<h1>修改失败！3秒后跳转首页。。。</h1>");
            response.setHeader("refresh","3;url=//localhost:8080/index.jsp");
        }






    }


    /**
     * 列表分页查询
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void PageHelperList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取当前页码
        String currentPage = request.getParameter("currentPage");

        int curPage = 1;    //第一次获取用户数据时，没有发送当前页码 (初始化为1)


        if (currentPage != null) {
            curPage = Integer.parseInt(currentPage);
        }


        //实例化Service层
        UserService userService = new UserService();
        //调用业务层功能
        PageInfo<User> pageInfo = userService.findUserByPage(curPage);


        //添加到域对象中
        request.setAttribute("pageInfo", pageInfo);

        //转发
        request.getRequestDispatcher("/list.jsp").forward(request, response);

    }


    /**
     * 添加用户
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //使用BeanUtil工具类 把浏览器获取到得到用户信息映射到Bean实体类中
        User user = CommonUtils.CommonPopulate(User.class, request);
        //实例化service
        UserService userService = new UserService();
        boolean result = userService.addUser(user);

        if (result) {
            //添加用户成功
            response.sendRedirect("/add_ok.jsp");
        } else {
            //添加失败
            request.setAttribute("error", "邮箱重复");
            request.getRequestDispatcher("/add_ok.jsp").forward(request, response);
        }

    }
}
